package com.block.source.service.receive;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.enums.BaseTypeInfoEnum;
import com.block.common.enums.InfoTypeEnum;
import com.block.common.model.vo.ResultInfoVO;
import com.block.common.service.IAddressInfoService;
import com.block.common.service.IApplicationInfoService;
import com.block.common.service.IChannelCommonService;
import com.block.db.entity.*;
import com.block.db.mapper.SourcePushResultInfoMapper;
import com.block.source.model.api.LianZongQianBaoParam;
import com.block.source.model.api.RenYiHuaParam;
import com.block.source.model.dto.SourceApiParam;
import com.block.source.model.dto.SourceApiResult;
import com.block.source.model.param.UserDataParam;
import com.block.source.service.ISourceReceiveService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class LianZongQianBaoReceiveService extends ISourceReceiveService {

    @Autowired
    IAddressInfoService addressInfoService;
    @Resource
    SourcePushResultInfoMapper sourcePushResultInfoMapper;
    @Autowired
    IChannelCommonService channelCommonService;
    @Autowired
    IApplicationInfoService applicationInfoService;


    @Override
    public SourceApiResult handleCheckUserData(SourceBaseInfo sourceBaseInfo, Map<String, String[]> parameterMap, String paramContent) {
        SourceApiResult sourceApiResult = new SourceApiResult();
        sourceApiResult.setSuccess(false);

        String apiSecret = sourceBaseInfo.getApiSecret();

        JSONObject jsonObjectParamContent = JSONObject.parseObject(paramContent);
        if (ObjectUtil.isEmpty(jsonObjectParamContent)) {
            sourceApiResult.setReturnObject(this.getError("撞库传入的信息为空"));
            return sourceApiResult;
        }

        UserDataParam userDataParam = this.getUserDataParam(paramContent);


        // 通过城市编码,获取城市信息
        AddressInfo addressInfo = addressInfoService.getAddresInfoByCity(userDataParam.getCity());
        if (ObjectUtil.isEmpty(addressInfo)) {
            sourceApiResult.setReturnObject(this.getError("城市信息为空"));
            return sourceApiResult;
        }

        // 设置省市信息


        String aesName = jsonObjectParamContent.getString("name");



        String Name = SecureUtil.aes(apiSecret.getBytes()).decryptStr(aesName);


        userDataParam.setRealName(Name);
        userDataParam.setCityCode(addressInfo.getCityCode());
        userDataParam.setCity(addressInfo.getCity());
        userDataParam.setProvinceCode(addressInfo.getProvinceCode());
        userDataParam.setProvince(addressInfo.getProvince());

        Integer randomInt = RandomUtil.randomInt(Integer.MAX_VALUE);
        userDataParam.setOutOrderNo(String.valueOf(randomInt));


        sourceApiResult.setUserDataParam(userDataParam);
        sourceApiResult.setSuccess(true);

        return sourceApiResult;
    }

    @Override
    public SourceApiResult handleRegisterUserData(SourceBaseInfo sourceBaseInfo, Map<String, String[]> parameterMap, String paramContent) {

        SourceApiResult sourceApiResult = new SourceApiResult();
        sourceApiResult.setSuccess(false);

        String apiSecret = sourceBaseInfo.getApiSecret();

        JSONObject jsonObjectParamContent = JSONObject.parseObject(paramContent);
        if (ObjectUtil.isEmpty(jsonObjectParamContent)) {
            sourceApiResult.setReturnObject(this.getError("进件传入的信息为空"));
            return sourceApiResult;
        }

        UserDataParam userDataParam = this.getUserDataParam(paramContent);

        // 通过城市编码,获取城市信息
        AddressInfo addressInfo = addressInfoService.getAddresInfoByCity(userDataParam.getCity());
        if (ObjectUtil.isEmpty(addressInfo)) {
            sourceApiResult.setReturnObject(this.getError("城市信息为空"));
            return sourceApiResult;
        }

        // 设置省市信息
        String aesPhoneMd5 = jsonObjectParamContent.getString("phone");
        String productId = jsonObjectParamContent.getString("productId");
        String aesName = jsonObjectParamContent.getString("name");

        String phone = SecureUtil.aes(apiSecret.getBytes()).decryptStr(aesPhoneMd5);
        String Name = SecureUtil.aes(apiSecret.getBytes()).decryptStr(aesName);

        userDataParam.setMobile(phone);
        userDataParam.setRealName(Name);
        userDataParam.setCityCode(addressInfo.getCityCode());
        userDataParam.setCity(addressInfo.getCity());
        userDataParam.setProvinceCode(addressInfo.getProvinceCode());
        userDataParam.setProvince(addressInfo.getProvince());


        SourcePushResultInfo pushResultInfo = sourcePushResultInfoMapper.selectByChannelCodeAndOutOrderNo(sourceBaseInfo.getChannelCode(), productId);

        userDataParam.setOrderNo(String.valueOf(pushResultInfo.getId()));

        sourceApiResult.setUserDataParam(userDataParam);
        sourceApiResult.setSuccess(true);

        return sourceApiResult;
    }

    @Override
    public SourceApiResult getCheckUserReturnData(SourceBaseInfo sourceBaseInfo, ResultInfoVO resultInfoVO, SourceApiParam sourceApiParam) {


        SourceApiResult sourceApiResult = new SourceApiResult();

        MerchantBaseInfo merchantBaseInfo = sourceApiParam.getMerchantBaseInfo();
        SourcePushResultInfo pushResultInfo = sourceApiParam.getSourcePushResultInfo();

        MerchantThirdBaseInfo thirdBaseInfo = sourceApiParam.getThirdBaseInfo();

        JSONObject returnObject = new JSONObject();
        returnObject.put("code", 200);
        returnObject.put("msg", "success！");
        returnObject.put("productId", pushResultInfo.getOutOrderNo());

        JSONObject data = new JSONObject();
        if (StrUtil.equals(merchantBaseInfo.getInfoType(), InfoTypeEnum.APIS.getValue())) {
            String companyLogo = thirdBaseInfo.getCompanyLogo();
            if (StringUtils.isEmpty(companyLogo)) {
                data.put("logo", "");
            } else {
                data.put("logo", companyLogo);
            }
            data.put("productName", thirdBaseInfo.getProductName());
            data.put("companyName", thirdBaseInfo.getCompanyName());
            data.put("price", pushResultInfo.getPriceAmount());
        }else {
            data.put("price", pushResultInfo.getPriceAmount());
            data.put("productName", merchantBaseInfo.getInfoName());
            data.put("logo", merchantBaseInfo.getCompanyLogo());
            data.put("companyName", merchantBaseInfo.getCompanyName());
        }
        returnObject.put("data", data);
        sourceApiResult.setReturnObject(returnObject);

        return sourceApiResult;
    }


    @Override
    public SourceApiResult getRegisterUserReturnData(SourceBaseInfo sourceBaseInfo, ResultInfoVO resultInfoVO, SourceApiParam sourceApiParam) {
        SourceApiResult sourceApiResult = new SourceApiResult();

        JSONObject returnObject = new JSONObject();

        JSONObject data = resultInfoVO.getData();
        String respCode = resultInfoVO.getRespCode();


        if (StrUtil.equals(respCode, "200") && resultInfoVO.isSuccess()) {
            String status = data.getString("status");
            {
                if (StrUtil.equals(status, "000")) {
                    returnObject = this.getSuccess();
                    returnObject.put("data", null);
                    sourceApiResult.setReturnObject(returnObject);
                    return sourceApiResult;
                }
            }
        }
        returnObject = this.getError("推送失败");
        returnObject.put("data", null);
        sourceApiResult.setReturnObject(returnObject);

        return sourceApiResult;
    }

    @Override
    public JSONObject getError(String message) {
        JSONObject returnObject = new JSONObject();
        returnObject.put("status", 500);
        returnObject.put("msg", message);
        return returnObject;
    }

    @Override
    public JSONObject getSuccess() {
        JSONObject returnObject = new JSONObject();
        returnObject.put("code", 200);
        returnObject.put("msg", "success");
        return returnObject;
    }

    @Override
    public UserDataParam getUserDataParam(String paramContent) {

        LianZongQianBaoParam lianzongqianbao = JSON.parseObject(paramContent, LianZongQianBaoParam.class);

        UserDataParam userDataParam = new UserDataParam();


        userDataParam.setAge(lianzongqianbao.getAge());
        userDataParam.setCity(lianzongqianbao.getCity());
        userDataParam.setSex(lianzongqianbao.getSex().toString());
        userDataParam.setUserIp(lianzongqianbao.getIp());
        userDataParam.setYongtu(BaseTypeInfoEnum.Yongtu.yt007.getValue());
        userDataParam.setXueli(BaseTypeInfoEnum.XueLi.xl001.getValue());
        userDataParam.setShouru(BaseTypeInfoEnum.Shouru.sr001.getValue());
        userDataParam.setWeili(BaseTypeInfoEnum.WeiLi.wl001.getValue());
        userDataParam.setMobileMd5(lianzongqianbao.getPhoneMd5());
        userDataParam.setXinyongka(BaseTypeInfoEnum.XinYongKa.xyk001.getValue());

        Integer baitiao = lianzongqianbao.getBaiTiao();
        if (StrUtil.equals(String.valueOf(baitiao), "1")) {
            userDataParam.setBaitiao(BaseTypeInfoEnum.Baitiao.bt002.getValue());
        } else {
            userDataParam.setBaitiao(BaseTypeInfoEnum.Baitiao.bt001.getValue());
        }

        Integer huabei = lianzongqianbao.getHuaBei();
        if (StrUtil.equals(String.valueOf(huabei), "1")) {
            userDataParam.setHuabei(BaseTypeInfoEnum.Huabei.hb002.getValue());
        } else {
            userDataParam.setHuabei(BaseTypeInfoEnum.Huabei.hb001.getValue());
        }


        Integer yuqi = lianzongqianbao.getCredit();
        if (StrUtil.equals(String.valueOf(yuqi), "0")) {
            userDataParam.setYuqi(BaseTypeInfoEnum.Yuqi.yq001.getValue());
        } else {
            userDataParam.setYuqi(BaseTypeInfoEnum.Yuqi.yq002.getValue());
        }

        Integer loan = lianzongqianbao.getMoney();
        if (StrUtil.equals(String.valueOf(loan), "50000")) {
            userDataParam.setLoanAmount(BaseTypeInfoEnum.LoanAmount.la002.getValue());
        } else if (StrUtil.equals(String.valueOf(loan), "100000")) {
            userDataParam.setLoanAmount(BaseTypeInfoEnum.LoanAmount.la003.getValue());
        } else if (StrUtil.equals(String.valueOf(loan), "150000")) {
            userDataParam.setLoanAmount(BaseTypeInfoEnum.LoanAmount.la004.getValue());
        }

        userDataParam.setLoanTime(BaseTypeInfoEnum.LoanTime.lt001.getValue());

        Integer zhiMa = lianzongqianbao.getSesameScore();
        if (StrUtil.equals(String.valueOf(zhiMa), "1")) {
            userDataParam.setZhima(BaseTypeInfoEnum.Zhima.zm005.getValue());
        } else if (StrUtil.equals(String.valueOf(zhiMa), "2")) {
            userDataParam.setZhima(BaseTypeInfoEnum.Zhima.zm004.getValue());
        } else if (StrUtil.equals(String.valueOf(zhiMa), "3")) {
            userDataParam.setZhima(BaseTypeInfoEnum.Zhima.zm003.getValue());
        } else  {
            userDataParam.setZhima(BaseTypeInfoEnum.Zhima.zm002.getValue());
        }

        Integer GJJ = lianzongqianbao.getFund();

        if (StrUtil.equals(String.valueOf(GJJ), "1")) {
            userDataParam.setGjj(BaseTypeInfoEnum.Gjj.gjj003.getValue());
        } else {
            userDataParam.setGjj(BaseTypeInfoEnum.Gjj.gjj001.getValue());
        }

        Integer SheBao = lianzongqianbao.getSocial();
        if (StrUtil.equals(String.valueOf(SheBao),"1")) {
            userDataParam.setShebao(BaseTypeInfoEnum.Shebao.sb003.getValue());
        } else {
            userDataParam.setShebao(BaseTypeInfoEnum.Shebao.sb001.getValue());
        }

        String baodan = lianzongqianbao.getInsurance().toString();

        if (StrUtil.equals(baodan, "1")) {
            userDataParam.setBaoxian(BaseTypeInfoEnum.Baoxian.bx003.getValue());
        } else {
            userDataParam.setBaoxian(BaseTypeInfoEnum.Baoxian.bx001.getValue());
        }
        String house = lianzongqianbao.getHouse().toString();

        if (StrUtil.equals(house, "1")) {
            userDataParam.setHouse(BaseTypeInfoEnum.House.hu003.getValue());
        } else {
            userDataParam.setHouse(BaseTypeInfoEnum.House.hu001.getValue());
        }
        String car = lianzongqianbao.getCar().toString();

        if (StrUtil.equals(car, "1")) {
            userDataParam.setCar(BaseTypeInfoEnum.Car.car002.getValue());
        } else {
            userDataParam.setCar(BaseTypeInfoEnum.Car.car001.getValue());
        }

        userDataParam.setGongzuo(BaseTypeInfoEnum.Gongzuo.gz001.getValue());

        return userDataParam;
    }

}
